package com.backend.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.backend.system.beans.DeleteBatchReq;
import com.backend.system.beans.DeleteReq;
import com.backend.system.beans.user.*;
import com.backend.system.common.PageBean;
import com.backend.system.common.RequestHeaderUserInfo;
import com.backend.system.common.RequestHolder;
import com.backend.system.common.ResponseEntity;
import com.backend.system.dao.entity.Menu;
import com.backend.system.dao.entity.Role;
import com.backend.system.dao.entity.User;
import com.backend.system.service.IMenuService;
import com.backend.system.service.IRoleService;
import com.backend.system.service.IUserService;
import com.backend.system.service.manage.UserManage;
import com.backend.system.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author backend-manage
 * @since 2024-04-02
 */
@Api(tags = "用户表 前端控制器")
@RestController
@RequestMapping("/sys/user")
public class UserController {

    @Resource
    private IUserService userService;

    @Resource
    private IRoleService roleService;

    @Resource
    private IMenuService menuService;


    /**
     * 用户登录
     */
    @ApiOperation("用户登录")
    @SaIgnore
    @PostMapping("/doLogin")
    public ResponseEntity<UserLoginVO> doLogin(@Valid @RequestBody UserLoginReq req) {
        return ResponseUtil.success(UserManage.userLogin(req));
    }

    /**
     * 获取当前用户信息
     */
    @SaCheckPermission("user-personal-get")
    @ApiOperation("获取当前用户信息")
    @PostMapping("/searchCurrentUserInfo")
    public ResponseEntity<UserInfoVO> searchCurrentUserInfo() {
        RequestHeaderUserInfo userInfo = RequestHolder.getUserInfo();
        User user = userService.getById(userInfo.getLoginId());

        UserInfoVO userInfoVo = new UserInfoVO();
        // 组装当前角色
        List<Role> roles = roleService.getRolesByUserId(user.getId());
        for (Role role : roles) {
            if (Objects.equals(role.getId(), userInfo.getRoleId())) {
                userInfoVo.setCurrentRoleId(userInfo.getRoleId());
                userInfoVo.setCurrentRoleCode(role.getCode());
                userInfoVo.setCurrentRoleName(role.getRoleName());
                break;
            }
        }

        // 组装当前角色对应的菜单信息
        List<Menu> menus = menuService.getMenusByRoleId(userInfoVo.getCurrentRoleId());
        userInfoVo.setMenuTree(menuService.menuToTree(menus));

        userInfoVo.setId(user.getId());
        userInfoVo.setLoginAccount(user.getLoginAccount());
        userInfoVo.setUsername(user.getUsername());
        userInfoVo.setCreateTime(user.getCreateTime());
        return ResponseUtil.success(userInfoVo);
    }


    /**
     * 退出登录
     */
    @SaIgnore
    @ApiOperation("退出登录")
    @PostMapping("/doLogout")
    public ResponseEntity<Boolean> doLogout() {
        RequestHeaderUserInfo userInfo = RequestHolder.getUserInfo();
        String token = userInfo.getToken();
        StpUtil.logoutByTokenValue(token);
        return ResponseUtil.success(true);
    }

    /**
     * 修改密码
     */
    @ApiOperation("修改密码")
    @SaCheckPermission("user-modify-pwd")
    @PostMapping("/modifyPassword")
    public ResponseEntity<Boolean> modifyPassword(@Valid @RequestBody UserModifyPasswordReq req) {
        return ResponseUtil.success(UserManage.userModifyPassword(req));
    }

    /**
     * 新增/修改用户
     */
    @ApiOperation("新增/修改用户")
    @SaCheckPermission("user-add-update")
    @PostMapping("/addOrUpdate")
    public ResponseEntity<Boolean> addOrUpdate(@Valid @RequestBody UserAddOrUpdateReq req) {
        return ResponseUtil.success(UserManage.addOrUpdate(req));
    }

    /**
     * 分页查询用户列表
     */
    @SaCheckPermission("user-get")
    @ApiOperation("分页查询用户列表")
    @PostMapping("/list")
    public ResponseEntity<PageBean<UserQueryVO>> listByPage(@Valid @RequestBody UserQueryReq req) {
        req.setDefaultPageInfo();
        return ResponseUtil.success(userService.listByPage(req));
    }

    /**
     * 分配角色
     */
    @ApiOperation("分配角色")
    @SaCheckPermission("user-grant-roles")
    @PostMapping("/grantRoles")
    public ResponseEntity<Boolean> grantRoles(@Valid @RequestBody UserGrantRolesReq req) {
        return ResponseUtil.success(UserManage.grantRoles(req));
    }

    /**
     * 删除用户
     */
    @ApiOperation("删除用户")
    @SaCheckPermission("user-delete")
    @PostMapping("/delete")
    public ResponseEntity<Boolean> delete(@Valid @RequestBody DeleteReq req) {
        return ResponseUtil.success(UserManage.delete(req));
    }

    /**
     * 批量删除用户
     */
    @ApiOperation("批量删除用户")
    @SaCheckPermission("user-delete")
    @PostMapping("/deleteBatch")
    public ResponseEntity<Boolean> deleteBatch(@Valid @RequestBody DeleteBatchReq req) {
        return ResponseUtil.success(UserManage.deleteBatch(req));
    }
}
